package io.github.talelin.latticy.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.model.AddressDO;
import io.github.talelin.latticy.service.AddressService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

/**
* @author generator@lms
* @since 2021-04-20
*/
@RestController
@RequestMapping("/api/address")
public class AddressApiController {

    @Autowired
    private AddressService addressService;

    @PostMapping("")
    public CreatedVO create(@RequestBody @Validated AddressDO dto) {
        boolean save = addressService.save(dto);
        System.out.println(save);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    public UpdatedVO update(
            @RequestBody @Validated AddressDO dto,
            @PathVariable @Positive(message = "{id.positive}") Integer id) {
        AddressDO address = addressService.getById(id);
        if (address == null) {
            throw new NotFoundException(80000);
        }
        BeanUtils.copyProperties(dto,address);
        address.setId(id);
        addressService.updateById(address);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        AddressDO address = addressService.getById(id);
        if (address == null) {
            throw new NotFoundException(80000);
        }
        addressService.removeById(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    public AddressDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        AddressDO address = addressService.getById(id);
        if (address == null) {
            throw new NotFoundException(80000);
        }
        return address;
    }

    @GetMapping("/page/{uid}")
    public PageResponseVO<AddressDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @PathVariable(value = "uid") @Positive(message = "{id.positive}") Integer uid
    ) {
        Page<AddressDO> pager = new Page<>(page, count);
        QueryWrapper<AddressDO> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        IPage<AddressDO> paging = addressService.getBaseMapper().selectPage(pager,queryWrapper);
        return PageUtil.build(paging);
    }


}
